package vn.com.vega.chacha.v2.Asynctask;

import java.util.ArrayList;
import java.util.List;

import vn.com.vega.chacha.R;
import vn.com.vega.chacha.v2.ActivityBase;
import vn.com.vega.music.clientserver.JsonSong;
import vn.com.vega.music.clientserver.JsonVideo;
import vn.com.vega.music.objects.Song;
import vn.com.vega.music.objects.Video;

public class FavoriteVideo extends BaseAsyncTask {
	DoSomthingListener doSomthingListener;
	Video video;
	String errorMessage;
	boolean favorite;

	public FavoriteVideo(ActivityBase base, Video video, DoSomthingListener listener, boolean doFavorite) {
		super(base);
		this.video = video;
		this.doSomthingListener = listener;
		this.favorite = doFavorite;
		// TODO Auto-generated constructor stub
	}

	@Override
	protected Void doInBackground(Void... params) {
		// TODO Auto-generated method stub
		JsonVideo jsonVideo;
		if (favorite) {
			jsonVideo = JsonVideo.addFavorite(video.id);
		} else {
			jsonVideo = JsonVideo.unFavorite(video.id);
		}
		if (jsonVideo.isSuccess()) {
			errorMessage = null;
			if (favorite) {
				video.isFavorite = true;
			} else {
				video.isFavorite = false;
			}
		} else {
			errorMessage = jsonVideo.getErrorMessage();
		}
		return null;
	}

	@Override
	protected void onPostExecute(Void result) {
		// TODO Auto-generated method stub
		if (!checkIsLive() || isCancelled()) {
			return;
		}
		ActivityBase act = baseActivityRfc.get();
		if (doSomthingListener != null) {
			List<Object> listExtractObject = new ArrayList<Object>();
			if (errorMessage == null) {
				listExtractObject.add(video);
				if (favorite) {
					listExtractObject.add(act.getString(R.string.add_video_to_list_favorite_success));
				} else {
					listExtractObject.add(act.getString(R.string.delete_video_from_list_favorite_success));
				}
				
				doSomthingListener.doSomethingSuccess(listExtractObject);
			} else {
				listExtractObject.add(video);
				listExtractObject.add(errorMessage);
				doSomthingListener.doSomethingSuccess(listExtractObject);
			}
		} 
	}

	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		super.onPreExecute();
		if (!checkIsLive()) {
			return;
		}
		ActivityBase act = baseActivityRfc.get();
		if (doSomthingListener != null) {
			List<Object> listExtract = new ArrayList<Object>();
			if (favorite) {
				listExtract.add(act.getString(R.string.adding_video_to_list_favorite));
			} else {
				listExtract.add(act.getString(R.string.delete_video_from_list_favorite));
			}
			doSomthingListener.startDoSomthing(listExtract);
		}
	}

}
